<?php
	if(isset($_REQUEST["key"]))
	{
		$key=$_REQUEST["key"];
		$type=$_REQUEST["type"];
		
		$sql="Select * From san_pham sp,gian_hang gh Where sp.sp_ma_gian_hang=gh.gh_ma and (sp.sp_ten like '%$key%' or sp.sp_mo_ta like '%$key%' or sp.sp_don_gia ='$key' or sp.sp_so_luong='$key')";
		//echo($sql);
		$sql1="Select count(*) as number From san_pham sp,gian_hang gh Where sp.sp_ma_gian_hang=gh.gh_ma and (sp.sp_ten like '%$key%' or sp.sp_mo_ta like '%$key%' or sp.sp_don_gia ='$key' or sp.sp_so_luong='$key')";
		$ngay="";
		$tong="";
		$price="";
		
		switch($type)
		{
			case 0:
				$sql=$sql." order by sp.sp_ngay_tao DESC, sp.sp_don_gia ASC";
				$ngay="selected='selected'";
				$tong="";
				$price="";
				break;
			case 1:
				$sql=$sql." order by sp.sp_tong_luot_danh_gia DESC, sp.sp_don_gia ASC";
				$ngay="";
				$tong="selected='selected'";
				$price="";
				break;
			case 2:
				$sql=$sql." order by sp.sp_don_gia ASC,sp.sp_tong_luot_danh_gia DESC";
				$ngay="";
				$tong="";
				$price="selected='selected'";
				break;

		}
		//echo($sql);
		require("DataHelper.php");
		$maxPage=0;
		$rowPerPage=10;
		$curPage=1;
		$maxItems=0;
		if($result=QuerySQL($sql1))
		{
		//echo($result);
			while($row=mysql_fetch_array($result))
			{
			//echo("2");
				$maxItems=$row["number"];
				$maxPage=ceil($maxItems/$rowPerPage);
			}		
		}
				
		
		if(isset($_REQUEST["page"]))
		{
				$curPage=$_REQUEST["page"];
		}
		$start=($curPage-1)*$rowPerPage;
		$end=($curPage-1)*$rowPerPage+$rowPerPage;
		$i=$start;
		$str="";
		if($maxItems==0)
		{
			$str="<hr/><h3>Không tìm thấy sản phẩm nào phù hợp</h3><br/>";
		}
		else
		{
			$str="<hr/><h3>Tìm thấy $maxItems sản phẩm phù hợp</h3><br/>";
			$str=$str."<label>Sắp xếp theo</label>
			<select  onchange='cbChange();' id='slid'>
                	<option value='0' $ngay>Ngày đăng</option>
                    <option value='1' $tong>Đánh giá nhiều</option>
                    <option value='2' $price>Giá</option>
                </select><br/><br/>";
		}
		$str=$str."<table>
				<tbody>";
		$sql=$sql." Limit $start,$rowPerPage";
		//echo($sql);		
		$result1=QuerySQL($sql);
		while($row=mysql_fetch_array($result1))
		{
		
			$ten=$row["sp_ten"];
			$ngaytao=$row["sp_ngay_tao"];
			$mota=$row["sp_mo_ta"];
			$dvtinh=$row["sp_don_vi_tinh"];
			$dongia=$row["sp_don_gia"];
			$hinhanh="images/".$row["sp_hinh_anh"];
			$soluong=$row["sp_so_luong"];
			$tinhtrang=$row["sp_tinh_trang"];
			$tongdanhgia=$row["sp_tong_luot_danh_gia"];
			$tongsao=$row["sp_tong_so_sao"];
			$magh=$row["sp_ma_gian_hang"];
			$tengh=$row["gh_ten"];
			
			
			$str=$str."<tr>
						<td style='align='center'>
							<img src='$hinhanh' with='100px' height='100px' alt='ảnh sản phẩm'/>
						</td>
						<td >
							<table>
							<tr>
								<td>
									<label class='stitle'>Tên :</label>
									<label class='sproduct'><a href='#'>$ten</a></label>
								</td>
							</tr>
							<tr>
								<td>
									<label class='stitle'>Gian hàng bán :</label>
									<label class='sproduct'><a href='#'>$tengh</a></label>
								</td>
							</tr>
							<tr>
								<td>
									<label class='stitle'>Đơn giá :</label>
									<label class='sproduct'>$dongia VND</label>
								</td>
							</tr>
							<tr>
								<td>
									<label class='stitle'>Số lượng còn :</label>
									<label class='sproduct'>$soluong $dvtinh</label>
								</td>
							</tr>
							<tr>
								<td>
									<label class='stitle'>Số lượt đánh giá :</label>
									<label class='sproduct'>$tongdanhgia</label>
								</td>
							</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td colspan='2'>
							<label class='stitle'>Mô tả :</label>
							<span>$mota</span>
							
						</td>
					</tr><br/><br/>";
				
		}
		$str=$str."</tbody>
			</table><br/>";
		if($maxItems>0)
		{
			$str=$str."<table><tr><td>Trang :";
			for($i=1;$i<=$maxPage;$i++)
			{
				if($i==$curPage)
				{
					$str=$str."<b id='currentPage' title='$i' style='color:red'>$i&nbsp;&nbsp;</b>";
				}
				else
				{
					$str=$str."<label onclick='indexClick($i);' style='text-decoration:none;cursor:pointer;'>$i&nbsp;&nbsp;</label>";
				}
			}
		}
		$str=$str."</table><input type='hidden' value='$key' id='keyhd' />";
		$str=$str."<input type='hidden' value='$type' id='typehd' />";
		echo($str);
	}
?>